<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>react-demo-18</title>
    <link rel="preconnect" href="https://fonts.googleapis.com" />
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
    <link href="../../../js/font.css" rel="stylesheet" />
  </head>
  <style lang="scss">
    * {
      font-family: "Noto Sans SC", sans-serif;
    }

    body {
      display: flex;
      justify-content: flex-end;
    }

    .title {
      display: inline-flex;
      border: 1px solid blue;
      border-radius: 3px;
    }
  </style>
  <body>
    <!-- 准备容器 -->
    <div id="main"></div>

    <!-- 引入react核心库 -->
    <script
      type="text/javascript"
      src="../../../js/react.development.min.js"
    ></script>
    <!-- 引入react-dom，用于操作dom -->
    <script
      type="text/javascript"
      src="../../../js/react-dom.development.min.js"
    ></script>

    <!-- 引入prop-types -->
    <script src="../../../js/prop-types.js"></script>

    <!-- 引入babel，用于编译jsx -->
    <script src="../../../js/babel.min.js"></script>

    <!-- 此处一定要写babel，用于提示babel对该内容进行编译 -->
    <script type="text/babel">
      class Demo extends React.Component {
        /*
          1. 通过onX属性指定事件处理函数
            1.1 React使用的是自定义封装原生事件，以达成兼容性。
            1.2 React中的事件是通过事件委托方式处理的，委托给组件最外层的元素
          2. 通过通过event.target得到发生事件的Dom元素对象
        */
        myRef = React.createRef();
        blurRef = React.createRef();

        alertData = () => {
          console.log(this.myRef);
          alert(this.myRef.current.value);
        };

        alertDataBlur = (event) => {
          alert(event.target.value);
        };

        render() {
          return (
            <div>
              <input
                ref={this.myRef}
                type="text"
                placeholder="Click to alert"
              />
              &nbsp;
              <button onClick={this.alertData}>Alert</button>&nbsp;
              <input
                onBlur={this.alertDataBlur}
                type="text"
                placeholder="Blur to alert"
              />
            </div>
          );
        }
      }
      ReactDOM.render(<Demo />, document.getElementById("main"));
    </script>
  </body>
</html>
